suffix := -loongson

all: test

# 部署环境
.PHONY: env apt
apt:
	sudo apt install python2 default-jdk

env: apt
	cd openblock-blocks$(suffix) && npm install 
	cd openblock-vm$(suffix) && npm install
	cd openblock-gui$(suffix) && npm install --force 
	cd openblock-l10n$(suffix) && npm install


#### 测试开发环境
.PHONY: link l10n block test clean
# 重新构建block
block:
	cd openblock-blocks$(suffix) && npm run prepublish

# 重新构建翻译
l10n: 
	cd openblock-l10n$(suffix) && npm run build 

link: l10n
	cd openblock-blocks$(suffix) && npm link 
	cd openblock-vm$(suffix) && npm link
	cd openblock-l10n$(suffix) && npm link
	cd openblock-gui$(suffix) && npm link \
		openblock-blocks openblock-vm openblock-l10n --force

test: link
	cd openblock-gui$(suffix) && npm start 

clean: 
	rm a.tar.gz
	cd openblock-gui$(suffix) && npm run clean
	cd openblock-blocks$(suffix) && npm run clean
	cd openblock-l10n$(suffix) && npm run clean


# 构建，测试和部署
.PHONY:run backend build 
backend:
	cd openblock-backend-loongson && make backend

build: # link
	cd openblock-gui$(suffix) && npm run build
	- rm -rf loongson-server/public/*
	- mkdir -p openblock-backend-loongson/loongson-server/public/
	cp -r openblock-gui$(suffix)/build/* openblock-backend-loongson/loongson-server/public/

run: build 
	cd openblock-backend-loongson && make run


# 部署到loogson运行
.PHONY: deploy-makefile deploy 
target := "root@ls2k.local"
tar = openblock-loongson.tar.gz
Makefile = Makefile-remote

deploy-makefile:
	scp $(Makefile) $(target):~/Makefile

Crontab := /var/spool/cron/crontabs/root
AutoScript = .openblock.sh
deploy-reboot:
	ssh $(target) "echo '@reboot /root/$(AutoScript)' >> $(Crontab)"


deploy: build 
	tar zcvf $(tar) openblock-backend-loongson 
	scp $(tar) $(target):~
	ssh $(target) "rm -rf openblock-backend-loongson"
	ssh $(target) "tar zxvf $(tar)"

	scp $(AutoScript) $(target):~
	ssh $(target) "chmod +x $(AutoScript)"
	rm $(tar)

deploy-obs: build
	obsutil cp openblock-gui-loongson/build/ obs://openblock -r -flat -f


# remote test 
.PHONY: remote-run remote-backend remote-server
# remote run backend
remote-run:
	ssh $(target) "cd openblock-backend-loongson && make run"

remote-backend: deploy-backend
	ssh $(target) 'make kill-backend'
	ssh $(target) "cd openblock-backend-loongson && python3 backend/main.py"

remote-server:
	ssh $(target) "cd openblock-backend-loongson && make server"


# git
.PHONY: git
action ?= add .
git:
	- cd openblock-blocks$(suffix) && git $(action)
	- cd openblock-vm$(suffix) && git $(action)
	- cd openblock-gui$(suffix) && git $(action)
	- cd openblock-l10n$(suffix) && git $(action)
	- cd openblock-backend-loongson && git $(action)
	- git $(action)